<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
          <el-card>
            <el-alert
                title="注意：只允许为第三级分类设置相关参数！"
                type="warning"
                show-icon
                :closable="false"
                effect="dark">
            </el-alert>
            <el-row>
                <el-col>
                    <span>选择商品分类：</span>
                    <el-cascader
                        v-model="selectKeys"
                        @change="onCascaderChange"
                        :options="cascaderOptions"
                        :props="cascaderProps"></el-cascader>
                </el-col>
            </el-row>
            <el-tabs v-model="activeName" type="border-card" @tab-click="changeTabClick">
                <el-tab-pane label="动态参数" name="many">
                    <el-button @click="addParamsVisible=true" :disabled="isBtnDisabled" type="primary" size="mini">
                        添加参数
                    </el-button>
                    <!--分类列表-->
                    <el-table
                        :data="manyTableData"
                        border
                        style="width: 100%">
<!--                        展开行-->
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-tag
                                    :key="index"
                                    v-for="(tag,index) in scope.row.attr_values"
                                    closable
                                    :disable-transitions="false"
                                    @close="onDeleteAttr(tag)">
                                    {{tag}}
                                </el-tag>
                                <el-input
                                    class="input-new-tag"
                                    v-if="scope.row.inputVisible"
                                    v-model="scope.row.inputValue"
                                    ref="saveTagInput"
                                    size="small"
                                    @keyup.enter.native="onAddAttr(scope.row)"
                                    @blur="onAddAttr(scope.row)"
                                >
                                </el-input>
                                <el-button v-else class="button-new-tag" size="small" @click="onShowAddAttrInput(scope.row)">+ New Tag</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="#" type="index"></el-table-column>
                        <el-table-column prop="attr_name" label="参数名称"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="onEditParams(scope.row)" icon="el-icon-edit">修改
                                </el-button>
                                <el-button type="danger" size="mini" @click=onDeleteParams(scope.row) icon="el-icon-delete">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="静态属性" name="only">
                    <el-button :disabled="true" type="primary" size="mini">
                        添加属性
                    </el-button>
                </el-tab-pane>
            </el-tabs>
        </el-card>
        <!--        添加参数-->
        <el-dialog title="添加动态参数" :visible.sync="addParamsVisible">
            <el-form :model="addParams" :rules="addParamsRules" ref="addParamsForm" label-width="100px">
                <el-form-item label="动态参数" prop="attr_name">
                    <el-input v-model="addParams.attr_name"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addGoodsCateVisible = false">取 消</el-button>
                <el-button type="primary" @click="onAddParams">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    created() {
        this.getCategoriesData();
    },
    data(){
        return{
            inputVisible: false,
            inputValue: "",
            activeName: 'many',
            cascaderOptions: [],
            cascaderProps: {
                expandTrigger: 'hover',
                value: "cat_id",
                label: "cat_name",
                children: "children"
            },
            selectKeys: [],
            manyTableData: [],
            onlyTableData: [],
            addParamsVisible: false,
            addParams: {
                attr_name: "",
                attr_sel: ""
            },
            addParamsRules: {
                attr_name: [
                    {required: true, message: '请输入分类名称', trigger: 'blur'},
                ]
            }
        }
    },
    methods: {
        async getCategoriesData() {
            let {data: res} = await this.axios.get("/categories")
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
            this.cascaderOptions = res.data;
        },
        changeTabClick() {
            console.log(this.activeName);
            this.getCategoriesAttrData()
        },
        onCascaderChange() {
            console.log("当前被选中的节点id", this.selectKeys);
            this.getCategoriesAttrData();
        },
        async getCategoriesAttrData(){
            if (this.selectKeys.length !== 3) {
                this.onlyTableData = [];
                this.manyTableData = [];
                this.selectKeys = [];
                return this.$message.warning("只允许为三级分类设置参数！")
            }
            let {data:res} = await this.axios.get(`/categories/${this.cateId}/attributes`, {
                params: {
                    sel: this.activeName
                }
            });
            console.log(res);
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
            res.data.forEach((item)=>{
                item.attr_values = item.attr_vals?item.attr_vals.split(","):[];
                item.inputVisible = false;
                item.inputValue = "";
            })
            if(this.activeName === "many"){
                this.manyTableData = res.data;
            }else {
                this.onlyTableData = res.data;
            }
        },
        onAddParams(){
            this.$refs.addParamsForm.validate(async (valid) => {
                if (!valid) return;
                let {data: res} = await this.axios.post(`/categories/${this.cateId}/attributes`, {
                    attr_name: this.addParams.attr_name,
                    attr_sel: this.activeName
                })
                this.addParams.attr_name = "";
                if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
                this.getCategoriesAttrData();
                this.$message.success(res.meta.msg);
                this.addParamsVisible = false;
            })
        },
        onEditParams(){},
        onDeleteParams(){},
        onShowAddAttrInput(row){
            row.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        onAddAttr(row){
            row.inputVisible = false;
        },
        onDeleteAttr(){
            console.log("删除参数的某一个属性")
        },
    },
    computed:{
        cateId(){
            return this.selectKeys.length === 3?this.selectKeys[2]:null
        },
        isBtnDisabled(){
            return this.selectKeys.length !== 3
        }
    },
    watch:{
        selectKeys(value, oldValue){
            console.log(value, oldValue);
        }
    }
}
</script>

<style scoped>
.el-row{
    margin-top: 20px;
    margin-bottom: 20px;
}
.el-tag{
    margin-left: 10px;
}
.input-new-tag{
    width: 90px;
    margin-left: 10px;
}
.button-new-tag{
    margin-left: 10px;
}
</style>